package jgl.vajra.driver.wxapi;

import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;

import jgl.vajra.driver.event.PayStatusEvent;
import jgl.vajra.driver.widget.utils.ToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends FragmentActivity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.pay_result);
//        api = PayManager.getInstance().msgApi;
        api = WXAPIFactory.createWXAPI(this, "wx68315131a0118617");

        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        int code = baseResp.errCode;
        switch (code) {
            case 0://支付成功后的界面
                ToastUtils.showShort("支付成功");
                EventBus.getDefault().post(new PayStatusEvent(true));
               // IntentManager.toPaySuccessActivity(this);
//                Constants.WEI_XIN_PAY_RESULT = 0; //这个是调起微信支付后，zai
                //返回主页面 然后在跳转至订单页面
                this.finish();
                break;
            case -1:
                //ToastUtil.showToast(getApplicationContext(), "签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、您的微信账号异常等。");
                ToastUtils.showShort("支付失败");
                EventBus.getDefault().post(new PayStatusEvent(false));
//                Constants.WEI_XIN_PAY_RESULT = -1;
                this.finish();
                break;
            case -2://用户取消支付后的界面
                ToastUtils.showShort("您已取消支付");
                EventBus.getDefault().post(new PayStatusEvent(false));
//                Constants.WEI_XIN_PAY_RESULT = -2;
                this.finish();
                break;
        }
    }
}
